package com.jdy.haoduoai.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdy.haoduoai.LeXiaoXiaoBanApp;
import com.jdy.haoduoai.intface.IAsyncTask;
import com.jdy.haoduoai.model.LeUser;
import com.jdy.haoduoai.model.ResultTestMain;
import com.jdy.haoduoai.parse.JsonSerializer;
import com.jdy.haoduoai.util.CommonUtil;
import com.jdy.haoduoai.util.CustomAsyncTask;
import com.jdy.haoduoai.util.HttpUtils;
import com.jdy.haoduoai.util.LeConfig;
import com.jdy.haoduoai.util.ResponseResult;
import com.jdy.haoduoai.util.Tools;
import com.jdy.haoduoai.view.CardPane;
import com.ycsj.goldmedalnewconcept.R;

import org.droidparts.annotation.inject.InjectView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

//import com.jdy.haoduoai.R;

/**
 * 成长测试说明页
 * 
 * @author hf
 * 
 */
public class AbilityTestExplainActivity extends BaseActivity implements
    OnClickListener {

  @InjectView(id = R.id.cardpane)
  private CardPane cardpane;
  @InjectView(id = R.id.begintest_btn, click = true)
  private Button begintest_btn;
  @InjectView(id = R.id.img_back, click = true)
  private View img_back;
  @InjectView(id = R.id.img_device, click = true)
  private ImageView img_device;
  @InjectView(id = R.id.tv_babyage)
  private TextView tv_babyage;
  @InjectView(id = R.id.img_exceed_age)
  private ImageView img_exceed_age;
  @InjectView(id = R.id.continue_lasttest, click = true)
  private ImageView continue_lasttest;

  private ResultTestMain mResultTestMain;

  @Override
  @SuppressLint("InflateParams")
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.abilitytest_explain);

    hideTitleView();

    showloading("");
    continue_lasttest.setOnTouchListener(CommonUtil.VIEW_TOUCH_DARK);
    loadInfo();
  }

  private void loadInfo() {
    new CustomAsyncTask(this, new IAsyncTask() {

      @Override
      public void onRecieveData(Activity activity, final ResponseResult result) {

        if (result.isSuccess()) {
          mResultTestMain = JsonSerializer.getInstance().deserialize(
              result.data, ResultTestMain.class);
        } else {
          toastShow(result.data);
        }

        if (Tools.isNotEmpty(mResultTestMain)) {
          tv_babyage.setText("宝宝当前年龄：" + mResultTestMain.age);
          cardpane.setNumber(mResultTestMain.test_counts);

          if (mResultTestMain.can_continue) {
            continue_lasttest.setVisibility(View.VISIBLE);
          }
        }

        dismissLoading();
      }

      @Override
      public ResponseResult doInbackground(Activity activity) {
        LeUser lu = LeXiaoXiaoBanApp.getInstance().getUser();
        if (lu == null) {
          lu = Tools.restoreLeUser();
        }

        HashMap<String, Object> param_ = new HashMap<String, Object>();
        param_.put(HttpUtils.TAG_SNO_I, lu.sno);

        return HttpUtils.startRequest(HttpUtils.API_URL
            + "/api/2.0/operation/growth_test/ready_start", param_,
            HttpUtils.TAG_GET_REQUEST);
      }
    }).execute();
  }

  @SuppressWarnings("unused")
  private void init() {
    LeUser lu = LeXiaoXiaoBanApp.getInstance().getUser();
    if (lu == null) {
      lu = Tools.restoreLeUser();
    }

    if (Tools.isNotEmpty(lu.machine)) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

      try {
        Calendar bef = Calendar.getInstance();
        bef.setTime(sdf.parse(lu.machine.dob));

        int month = Calendar.getInstance().get(Calendar.MONTH)
            - bef.get(Calendar.MONTH);

        int year = Calendar.getInstance().get(Calendar.YEAR)
            - bef.get(Calendar.YEAR);

        int totalMonth = year * 12 + month;
        if (totalMonth == 0) {
          totalMonth = 1;
        }

        if (totalMonth < 12) {
          tv_babyage.setText("宝宝当前年龄：" + totalMonth + "个月");
        } else {
          int year1 = totalMonth / 12;
          int month1 = totalMonth % 12;

          if (month1 > 0) {
            tv_babyage.setText("宝宝当前年龄：" + year1 + "岁" + month1 + "个月");
          } else {
            tv_babyage.setText("宝宝当前年龄：" + year1 + "岁");
          }

          if (year1 >= 6) {
            img_exceed_age.setVisibility(View.VISIBLE);
            begintest_btn.setVisibility(View.GONE);
          } else {
            img_exceed_age.setVisibility(View.GONE);
            begintest_btn.setVisibility(View.VISIBLE);
          }

        }
      } catch (Exception e) {
        // TODO: handle exception
      }
    }
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.continue_lasttest:
      Intent intent = new Intent();
      intent.putExtra("continuetest", true);
      intent.setClass(this, AbilityTestActivity.class);
      startActivity(intent);

      finish();
      break;
    case R.id.begintest_btn:
      startActivity(new Intent(this, AbilityTestActivity.class));
      finish();
      break;
    case R.id.img_back:
      this.finish();
      break;
    case R.id.img_device:
      if (LeConfig.isLogin) {
        startActivity(new Intent(this, DevicePropertyActivity.class));
      } else {
        Intent intent0 = new Intent(this, LoginActivity.class);
        intent0.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent0);
      }
      break;
    default:
      break;
    }
  }
}
